import axios from "axios";


export default defineBackground(async () => {
  //console.log('Hello background!', { id: browser.runtime.id });

  let action 
  if(browser.action) {
    action = browser.action
  }else if(browser.browserAction) {
    action = browser.browserAction
  }
  
 
  let _token = await storage.getItem('local:_token')
  if (_token && action) {
    // 添加记录书签相应
    let host = await storage.getItem('local:host')
    const url = host + '/web/add?_token=' + _token
    action.onClicked.addListener(async (tab) => {
      console.clear()
      console.info("post data")
      let res = await axios.post(url, { uri: tab.url, title: tab.title })
      if (res.data.errCode != 0) {
        action.setBadgeBackgroundColor({color: [255, 0, 0, 255]});
        action.setBadgeText({ text: "fail" })
      } else {
        action.setBadgeBackgroundColor({color: [0, 255, 0, 255]});
        action.setBadgeText({ text: "succ" })
      }
      setTimeout(() => {
        action.setBadgeBackgroundColor({color: [255, 255, 255, 0]});
        action.setBadgeText({ text: "" })
      }, 11000)
    })
  }
});
